package com.centaline.androidsalesblog.activities.sale;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.activities.news.ChatActivity;
import com.centaline.androidsalesblog.adapter.AgentCommentAdapter;
import com.centaline.androidsalesblog.api.sale.AgentCommentApi;
import com.centaline.androidsalesblog.base.BaseActivity;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.iml.RecyclerViewItemOnClickListener;
import com.centaline.androidsalesblog.model.businissobject.AgentCommentBo;
import com.centaline.androidsalesblog.model.databaseobject.BizUnitDo;
import com.centaline.androidsalesblog.model.responseobject.AgentCommentRo;
import com.centaline.androidsalesblog.rx.database.RxDataBase;
import com.centaline.androidsalesblog.utils.ContactUtil;
import com.centaline.androidsalesblog.utils.RongUserIdUtil;
import com.centaline.androidsalesblog.widgets.MRecyclerView;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.interfaces.IRecycleViewListener;
import com.centanet.framework.interfaces.IResponseListener;
import com.trello.rxlifecycle.ActivityEvent;

import java.util.ArrayList;
import java.util.Locale;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 购房指引
 */
public class AgentCommentActivity extends BaseActivity {

    private MRecyclerView mRecyclerView;

    private ArrayList<AgentCommentBo> list = new ArrayList<>();
    private AgentCommentAdapter agentCommentAdapter;
    private int iRefreshType;
    private AgentCommentApi agentCommentApi;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_ageny_comment;
    }

    @Override
    protected void findViews() {
        setUniversalToolbar(R.string.title_agent_comment);
        mRecyclerView = (MRecyclerView) findViewById(R.id.mRecyclerView);
    }

    @Override
    protected void initViews() {
        agentCommentAdapter = new AgentCommentAdapter(this, GlideLoad.initRound(this), list);
        agentCommentAdapter.setItemOnClickListener(new RecyclerViewItemOnClickListener<AgentCommentBo>() {
            @Override
            public void onItemOnClick(final AgentCommentBo agentCommentBo, int tag) {
                switch (tag) {
                    case 0:
                        startActivity(new Intent(AgentCommentActivity.this, ChatActivity.class)
                                .putExtra(ExtraConstant.TargetId, RongUserIdUtil.getSatffId(agentCommentBo.getStaffNo())));
                        break;
                    case 1:
                        ContactUtil.sendMsg(AgentCommentActivity.this, agentCommentBo.getStaffMobile());
                        break;
                    case 2:
                        new AlertDialog.Builder(AgentCommentActivity.this)
                                .setMessage(String.format(Locale.CHINA, "拨打:%s,%s",
                                        agentCommentBo.getBigCode(), agentCommentBo.getExtCode()))
                                .setPositiveButton("拨打", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        ContactUtil.dial(AgentCommentActivity.this, agentCommentBo.getBigCode(), agentCommentBo.getExtCode());

                                    }
                                }).setNegativeButton("取消", null)
                                .create()
                                .show();
                        break;
                    case 3:
                        startActivity(new Intent(AgentCommentActivity.this, StaffDetailActivity.class)
                                .putExtra(ExtraConstant.StaffNo, agentCommentBo.getStaffNo()));
                        break;
                    default:
                        break;
                }
            }
        });
        mRecyclerView.setAdapter(agentCommentAdapter);
        mRecyclerView.setIRecycleViewListener(new IRecycleViewListener() {
            @Override
            public void downRefresh() {
                iRefreshType = 1;
                agentCommentApi.setStartIndex(0);
                request();
            }

            @Override
            public void upRefresh() {
                iRefreshType = 2;
                agentCommentApi.setStartIndex(list.size());
                request();
            }

            @Override
            public void onItemClick(int position) {

            }
        });
        agentCommentApi = new AgentCommentApi(AgentCommentActivity.class, AgentCommentRo.class, new IResponseListener<AgentCommentRo>() {
            @Override
            public void onResponse(AgentCommentRo agentCommentRo) {
                final ArrayList<AgentCommentBo> temp = agentCommentRo.getList();
                if (iRefreshType == 1)
                    list.clear();
                if (temp == null) {
                    mRecyclerView.refreshError();
                } else {
                    list.addAll(agentCommentRo.getList());
                    mRecyclerView.stopRefresh(agentCommentRo.getList().size() == 10);
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                mRecyclerView.refreshError();
            }
        });
        bizUnit();
        agentCommentApi.setPostId(getIntent().getStringExtra(ExtraConstant.PostId));
        mRecyclerView.startRefresh();
    }

    private void request() {
        agentCommentApi.startRequest();
    }

    private void bizUnit() {
        RxDataBase.getBizUnit()
                .compose(this.<BizUnitDo>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BizUnitDo>() {
                    @Override
                    public void call(BizUnitDo bizUnitDo) {
                        agentCommentAdapter.setDescriptionSplitChar(bizUnitDo.isShowTalk(),
                                bizUnitDo.getDescriptionSplitChar());
                    }
                });
    }
}
